package com.qqtech.qquan.community.controller;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.enums.CommunityNoticeStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityNotice;
import com.qqtech.qquan.community.model.CommunityNoticeQuery;
import com.qqtech.qquan.community.model.CommunityNoticeVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityNoticeService;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/community/notice")
public class CommunityNoticeController extends BaseWebController<CommunityNotice, CommunityNoticeQuery> {
	@Resource
	private CommunityNoticeService communityNoticeService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private UserBaseService userBaseService;
	
	@Override
	protected BaseService<CommunityNotice> getBaseService() {
		return communityNoticeService;
	}

	/**
	 * 列表
	 * 
	 * @param communityNoticeQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody CommunityNoticeQuery communityNoticeQuery) {
		Result result = new Result();
		communityNoticeQuery.setStatus(CommunityNoticeStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(communityNoticeQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityNotice> page = communityNoticeService.queryPageList(communityNoticeQuery, pageKit);
		result.put("page", page);
		return result;
	}

	/**
	 * 管理列表
	 * 
	 * @param communityNoticeQuery
	 * @return
	 */
	@RequestMapping(value = "/manage/page")
	@ResponseBody
	public Result managePage(@RequestBody CommunityNoticeQuery communityNoticeQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		communityNoticeQuery.setCreatorId(curUserId);
		communityNoticeQuery.setStatus(CommunityNoticeStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(communityNoticeQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityNotice> page = communityNoticeService.queryPageList(communityNoticeQuery, pageKit);
		result.put("page", page);
		return result;
	}

	/**
	 * pv+1在获取详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody CommunityNoticeQuery communityNoticeQuery) {
		Result result = new Result();
		CommunityNoticeVo detailVo = communityNoticeService.handleDetail(communityNoticeQuery.getId());
		result.put("detailVo", detailVo);
		return result;
	}
	/**
	 * 更改
	 * 
	 * @param communityMessage
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(@RequestBody CommunityNotice communityNotice) {
		Result result = new Result();
		communityNoticeService.updateById(communityNotice);
		return result;
	}

	/**
	 * 更改
	 * 
	 * @param communityMessage
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	@ResponseBody
	public Result toUpdate(@RequestBody CommunityNoticeVo communityNoticeVo) {
		Result result = new Result();
		communityNoticeVo = communityNoticeService.getById(communityNoticeVo.getId());
		CommunityBaseVo communityBaseVo = communityBaseService.getById(communityNoticeVo.getCommunityId());
		communityNoticeVo.setCommunityName(communityBaseVo.getName());
		result.put("communityNoticeVo", communityNoticeVo);
		return result;
	}

	/**
	 * 新增
	 * 
	 * @param communityNotice
	 * @return
	 */
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody CommunityNotice communityNotice) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		communityNotice.setCreatorId(curUserId);
		communityNotice.setImg("");
		communityNotice.setPv(0);
		communityNotice.setStatus(CommunityNoticeStatusDictEnum.NORMAL.getCode());
		communityNotice.setIsShow(YesNoEnum.YES.getCode());
		communityNoticeService.insert(communityNotice);
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestBody CommunityNotice communityNotice) {
		Result result = new Result();
		communityNotice.setIsShow(YesNoEnum.NO.getCode());
		communityNotice.setStatus(CommunityNoticeStatusDictEnum.DELETE.getCode());
		communityNoticeService.updateById(communityNotice);
		return result;
	}
}
